package cn.afactory.tygz.ui.base.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.harwkin.nb.camera.BaseImgView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import org.w3c.dom.Text;

import cn.afactory.tygz.R;
import cn.afactory.tygz.common.Gendar;
import cn.afactory.tygz.common.UserListType;
import cn.afactory.tygz.db.DBManager;
import cn.afactory.tygz.model.UserInfo;
import cn.afactory.tygz.net.NetManager;
import cn.afactory.tygz.net.lsn.OnResponseListener;
import cn.afactory.tygz.ui.AppSettingsActivity;
import cn.afactory.tygz.ui.activities.MyMoneyActivity;
import cn.afactory.tygz.ui.adapter.SysMessageAdapter;
import cn.afactory.tygz.ui.base.BaseFragment;
import cn.afactory.tygz.ui.base.utils.DateUtil;
import cn.afactory.tygz.ui.base.utils.NaviUtils;
import cn.afactory.tygz.ui.base.utils.SPUtils;
import cn.afactory.tygz.ui.base.utils.ToastUtils;
import cn.afactory.tygz.ui.base.views.MineMenuItem;
import cn.afactory.tygz.ui.fragments.BaseTabFragment;

/**
 * Created with Android Studio.
 * Title:MineFragment
 * Description:
 * Copyright:Copyright (c) 2014
 * Company:中国平安健康保险有限公司
 * Author:Alice
 * Date:15/9/1
 * Time:下午11:34
 * Version 1.0
 */
public class MineFragment extends BaseTabFragment {
    private ImageView iv_head_icon;
    private TextView tv_user_name;
    private ImageView iv_user_gendar;
    private ImageView iv_user_jb;
    //    private TextView tv_user_money;
//    private TextView tv_user_level;
    private TextView tv_user_followed_count;
    private TextView tv_user_fans_count;
    private LinearLayout focus_ll;
    private LinearLayout fans_ll;


    private MineMenuItem menu_wdqy;
    private MineMenuItem menu_wdfb;
    private MineMenuItem menu_wgzdpd;
    private MineMenuItem menu_wczdtz;
    private MineMenuItem menu_wdgq;
    private MineMenuItem menu_wdsz;

    private RelativeLayout profile_top_layout;
    private boolean isHaveSign = false;
    private UserInfo userInfo;
    //徽章
    private TextView tv_tag_name;
    private LinearLayout ll_tag;
    protected ImageView iv_tag;

    @Override
    public View onCreateViewCustom(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_mine, null);
        initViews(view);

        setTitle(mContext.getString(R.string.title_mine));

        refreshSignStatus();

        return view;
    }

    /**
     * 刷新签到状态
     */
    private void refreshSignStatus(){
        String time = DateUtil.getRequestDateNotTime(System.currentTimeMillis());
        if (time.equals(SPUtils.getString(mContext, "signTime"))) {
            isHaveSign = true;
            setRightButton(mContext.getString(R.string.label_btn_have_check_in), new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    doCheckIn();
                }
            });
        } else {
            isHaveSign = false;
            setRightButton(mContext.getString(R.string.label_btn_check_in), new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    doCheckIn();
                }
            });
        }
    }

    @Override
    public void onResume() {
        initDatas();
        super.onResume();
    }

    //签到
    private void doCheckIn() {
        if (isHaveSign) {
            ToastUtils.show(mContext, "今天已签了哦,明天继续!");
            return;
        }
        NetManager.getInstance(mContext).doSign(new OnResponseListener<Integer>() {
            @Override
            public void onComplete(boolean isOK, Integer result, int errorCode, String errorMsg) {
                if (isOK) {
                    String signTime = DateUtil.getRequestDateNotTime(System.currentTimeMillis());
                    ToastUtils.show(mContext, "签到成功,明天继续哦");
                    userInfo.money = result;
                    DBManager.getInstance(getActivity().getApplicationContext()).saveUserInfo(userInfo);
//                    tv_user_money.setText(userInfo.money + "");
                    SPUtils.save(mContext, "signTime", signTime);
                    refreshSignStatus();
                } else {
                    ToastUtils.show(mContext, errorMsg);

                }
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                ToastUtils.show(mContext, errorMessage);

            }
        });
    }

    private void initDatas() {
        userInfo = DBManager.getInstance(getActivity().getApplicationContext()).getDefaultUserInfo();
        if (userInfo.userIcon != null) {
            BaseImgView.loadimg(iv_head_icon, userInfo.userIcon,
                    R.drawable.ic_pic_loading_default_small,
                    R.drawable.ic_pic_loading_default_small,
                    R.drawable.ic_pic_loading_default_small,
                    ImageScaleType.EXACTLY,
                    (int) mContext.getResources().getDimension(R.dimen.healthcircle_padding30),
                    (int) mContext.getResources().getDimension(R.dimen.healthcircle_padding30),
                    0);
        } else {
            iv_head_icon.setImageResource(R.drawable.ic_pic_loading_default_small);
        }

        if (userInfo.nickName != null) {
            tv_user_name.setText(userInfo.nickName);
        }

        if (userInfo.sex == Gendar.MALE) {
            iv_user_gendar.setImageResource(R.drawable.tag_male);
        } else if (userInfo.sex == Gendar.FEMALE) {
            iv_user_gendar.setImageResource(R.drawable.tag_female);
        } else if (userInfo.sex == Gendar.OTHER) {
            iv_user_gendar.setImageResource(R.drawable.tag_female);
        }

//        tv_user_money.setText(userInfo.money + "");

//        tv_user_level.setText(userInfo.level + "级");

        tv_user_fans_count.setText(String.format(getString(R.string.label_my_fans), userInfo.fans));

        tv_user_followed_count.setText(String.format(getString(R.string.label_my_followed), userInfo.focus));

        initTags();
    }

    private void initViews(View view) {
        focus_ll = (LinearLayout) view.findViewById(R.id.focus_ll);
        focus_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NaviUtils.gotoUserList(mContext,
                        SPUtils.getUid(mContext),
                        getString(R.string.title_my_followed_fans),
                        -1,
                        -1,
                        UserListType.MY_IDOLS);
            }
        });
        fans_ll = (LinearLayout) view.findViewById(R.id.fans_ll);
        fans_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NaviUtils.gotoUserList(mContext,
                        SPUtils.getUid(mContext),
                        getString(R.string.title_my_fans),
                        -1,
                        -1,
                        UserListType.MY_FANS);
            }
        });
        iv_head_icon = (ImageView) view.findViewById(R.id.iv_head_icon);
        tv_user_name = (TextView) view.findViewById(R.id.tv_user_name);
        iv_user_gendar = (ImageView) view.findViewById(R.id.iv_user_gendar);
        iv_user_jb = (ImageView) view.findViewById(R.id.iv_user_jb);
//        tv_user_money = (TextView)view.findViewById(R.id.tv_user_money);
//        tv_user_level = (TextView)view.findViewById(R.id.tv_user_level);
        tv_user_followed_count = (TextView) view.findViewById(R.id.tv_user_followed_count);
        tv_user_fans_count = (TextView) view.findViewById(R.id.tv_user_fans_count);

        menu_wdqy = (MineMenuItem) view.findViewById(R.id.menu_wdqy);
        menu_wdfb = (MineMenuItem) view.findViewById(R.id.menu_wdfb);
        menu_wgzdpd = (MineMenuItem) view.findViewById(R.id.menu_wgzdpd);
        menu_wczdtz = (MineMenuItem) view.findViewById(R.id.menu_wczdtz);
        menu_wdgq = (MineMenuItem) view.findViewById(R.id.menu_wdgq);
        menu_wdsz = (MineMenuItem) view.findViewById(R.id.menu_wdsz);

        profile_top_layout = (RelativeLayout) view.findViewById(R.id.profile_top_layout);

        tv_tag_name = (TextView) view.findViewById(R.id.tv_tag_name);
        ll_tag = (LinearLayout) view.findViewById(R.id.ll_tag);
        iv_tag = (ImageView) view.findViewById(R.id.iv_tag);

        menu_wdqy.initItem(R.drawable.ic_my_wdqy, R.string.label_menu_wdqy, -1);
        menu_wdfb.initItem(R.drawable.ic_my_wdfb, R.string.label_menu_wdfb, -1);
        menu_wgzdpd.initItem(R.drawable.ic_my_wdzdpd, R.string.label_menu_wgzdpd, -1);
        menu_wczdtz.initItem(R.drawable.ic_my_wdzdtz, R.string.label_menu_wczdtz, -1);
        menu_wdgq.initItem(R.drawable.ic_my_wdgq, R.string.label_menu_wdgq, -1);
        menu_wdsz.initItem(R.drawable.ic_my_wdsz, R.string.label_menu_wdsz, -1);

        menu_wdqy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoWdqy();
            }
        });

        menu_wdfb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoWfbdht();
            }
        });

        menu_wgzdpd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoWgzdht();
            }
        });

        menu_wczdtz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoWczdtz();
            }
        });

        menu_wdgq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoWdgq();
            }
        });

        menu_wdsz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoWdsz();
            }
        });

        profile_top_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NaviUtils.gotoUserInfoUpdatePage(getActivity());
            }
        });
    }

    /**
     *
     */
    private void initTags() {
        if (userInfo == null) {
            return;
        }
        if (userInfo.tagType > 0) {
            ll_tag.setVisibility(View.VISIBLE);
        } else {
            ll_tag.setVisibility(View.GONE);
        }
        cn.afactory.tygz.ui.base.utils.ViewUtils.applyTagView(userInfo.tagType, iv_tag, ll_tag);
        if (userInfo.tagList != null && userInfo.tagList.size() > 0) {
            cn.afactory.tygz.ui.base.utils.ViewUtils.applyTagName(userInfo.tagList, tv_tag_name);
        }
    }

    /**
     * 跳转到我的圈友
     */
    private void gotoWdqy() {
        NaviUtils.gotoMyFriends(getActivity());
    }

    /**
     * 发布的二手物品
     */
    private void gotoWfbdht() {
        NaviUtils.gotoMyPublishSecondGoodsPage(getActivity());
    }

    /**
     * 我关注的话题列表
     */
    private void gotoWgzdht() {
        NaviUtils.gotoTopicList(getActivity());
    }

    /**
     * 我参与的帖子
     */
    private void gotoWczdtz() {
        NaviUtils.gotoMyFollowedSubjectListPage(getActivity());
    }

    /**
     * 我的工钱
     */
    private void gotoWdgq() {

        getActivity().startActivity(new Intent(getActivity(), MyMoneyActivity.class));

    }

    /**
     * 我的设置
     */
    private void gotoWdsz() {
        getActivity().startActivity(new Intent(getActivity(), AppSettingsActivity.class));
    }
}
